package com.daxia.ogmall.web.controller.m;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.daxia.ogmall.common.MyJSONObject;
import com.daxia.ogmall.dto.DistrictDTO;
import com.daxia.ogmall.dto.api.DistrictAPIDTO;
import com.daxia.ogmall.dto.api.info.DistrictInfoAPIDTO;
import com.daxia.ogmall.support.MPage;

@Controller
@RequestMapping(value = "/m/district", produces = "application/json;charset=UTF-8")
public class MDistrictController extends MBaseController {
    
    @ResponseBody
    @RequestMapping("list")
    public String list(DistrictDTO dto, MPage page) throws Exception {
    
        List<DistrictDTO> districtDTOs = districtService.find(dto, page);

        return toApiOutput(districtDTOs, DistrictAPIDTO.class, DistrictInfoAPIDTO.class, page);
    }
    
    
    @ResponseBody
    @RequestMapping("detail")
    public String detail(DistrictDTO dto, MPage page) throws Exception {
    	MyJSONObject data = new MyJSONObject();
    	JSONArray districtInfos = new JSONArray();
    	List<DistrictDTO> list = districtService.find(dto, page);
    	for (DistrictDTO d : list) {
    		MyJSONObject districtInfo = new MyJSONObject();
    		districtInfo.put("id", d.getId());
    		districtInfo.put("name", d.getName());
    		
    		MyJSONObject provinceInfo = new MyJSONObject();
    		provinceInfo.put("id", d.getCity().getProvince().getId());
    		provinceInfo.put("name", d.getCity().getProvince().getName());
    		districtInfo.put("provinceInfo", provinceInfo);
    		
    		MyJSONObject cityInfo = new MyJSONObject();
    		cityInfo.put("id", d.getCity().getId());
    		cityInfo.put("name", d.getCity().getName());
    		districtInfo.put("cityInfo", cityInfo);
    		districtInfos.add(districtInfo);
    	}
    	
    	data.put("districtInfos", districtInfos);
		return toJson(data);
    }
    
}
